/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.ddtroops.security;

import cz.muni.fi.pa165.ddtroops.daoclasses.UserDAOImpl;
import cz.muni.fi.pa165.ddtroops.daointerfaces.BaseDAO;
import cz.muni.fi.pa165.ddtroops.daointerfaces.UserDAO;
import cz.muni.fi.pa165.ddtroops.entities.User;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 *
 * @author newohybat
 */
@Service
@Qualifier("customUserDetailsService")
public class DDTroopsUserDetailsService implements UserDetailsService{
    @Autowired(required=true)
    private UserDAO dao;
    public void setDao(UserDAO dao) {
        this.dao = dao;
    }
    public UserDetails loadUserByUsername(String string) throws UsernameNotFoundException {
        User found = dao.getByName(string);
        if(found==null) throw new UsernameNotFoundException("User with given name is not present within the system.");
        List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
        auths.add(new DDTroopsUserGrantedAuthority(found.getAuthority()));

        DDTroopsUserDetails userDetails = new DDTroopsUserDetails(found.getId(),found.getName(),found.getPassword(),auths);
        return userDetails;
    }

}
